import getUserInfo from '../../utils/getUserInfo'
import TimeShow from '../../utils/TimeShow'
import deleteWei from '../../utils/deleteWei'
Page({
  data: {
    pinglun: [],
    userInfo: wx.getStorageSync('userInfo')
  },

  onLoad: function (options) {
    this.getPingLun()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  // 获得评论消息
  async getPingLun() {
    let pinglun = wx.getStorageSync('pinglun')
    if (pinglun && pinglun.length > 0) {
      let ids = []
      pinglun.forEach((item) => {
        if(!ids.includes(item.userId)) {
          ids.push(item.userId)
        }
      })
      const users = await getUserInfo(ids)
      let hids = []
      pinglun.forEach(item => {
        if(item.isweidu) {
          getApp().globalData.wdxx -= 1
          if(!hids.includes(item.userId) && item['isku']) hids.push(item.userId)
        }
          if(!item['headpic'] && !item['username']) {
            users.forEach(ele => {
              if(ele.id == item.userId) {
                item['headpic'] = ele.headpic
                item['username'] = ele.username
              }
            })
          }
          item['time'] = TimeShow(item['stime'])
          item.isweidu = false
      })
      if(hids.length > 0) deleteWei(hids)
      pinglun.reverse()
      wx.setStorageSync('pinglun', pinglun)
      this.setData({
        pinglun
      })
    }
  },

  // 跳转到用户详情页
  hnadUser(e) {
    console.log(e)
    const { chatid, username } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/demes/demes?chatId=${chatid}&username=${username}`
    })
  }
})
